import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2024-06-21
 * Time: 10:16
 */

class Student implements Comparable<Student> {
    public String name;
    public int age;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
       /* if(this.age > o.age){
            return 1;
        }else if(this.age == o.age){
            return 0;
        }else{
            return -1;
        }*/
        return this.age - o.age;
        //return this.name.compareTo(o.name);
    }
}



public class Test {

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0]  = new Student("zhangsan",10);
        students[1]  = new Student("lisi",19);
        students[2]  = new Student("abc",8);
        System.out.println("排序前：" + Arrays.toString(students));
        //mySort(students);
        Comparator comparator = new NameComparator();
        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(students,ageComparator);
        System.out.println("排序后：" + Arrays.toString(students));

    }

    public static void mySort(Comparable[] comparables){
        for (int i = 0; i < comparables.length - 1; i++) {
            for (int j = 0; j < comparables.length - 1 - i; j++) {
                if(comparables[j].compareTo(comparables[j+1]) > 0){
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }
        }
    }
}



